home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1987 / 12 / holub / holub.exp < prev    next >
Text File  |  1979-12-31  |  6KB  |  240 lines

  1.  
  2.        idle()
  3.        {
  4.          int t, i ;
  5.  
  6.          do {
  7.            for( i = 1000; --i>=0 ; )
  8.                ;
  9.            t_cli();               /* Clear interrupts */
  10.            t = T_numtasks;        /* t = # of tasks  */
  11.            t_sti();               /* restore ints.  */
  12.          }
  13.          while( t > 1 ) ;
  14.  
  15.          t_delete( NULL );      /* delete self   */
  16.        }
  17.  
  18. Example 1: A typical idle task
  19.  
  20.  
  21.  
  22.  
  23.       foo( a, b )
  24.       char *a, *b;
  25.       {
  26.         t_printf("%s %s\n", a, b );
  27.         t_delete( NULL );
  28.       }
  29.  
  30.       main()
  31.       {
  32.         t_create( foo,"foo", 10, 512, "hello", "world", NULL);
  33.         t_start( 2 );
  34.       }
  35.  
  36. Example 2: An example of task creation
  37.  
  38.  
  39.  
  40.  
  41.     #include <stdio.h>
  42.     #include <tools/video.h>
  43.     #include "kernel.h"
  44.  
  45.     #define TEST 3 /* 1 = nonpreemptive test,
  46.                     * 2 = preemptive test: simple timer
  47.                     * 3 = test round-robin scheduling
  48.                     */
  49.  
  50.     T_QUEUE *Queue1;
  51.     T_QUEUE *Queue2;
  52.  
  53.     main()
  54.     {
  55.       int status = 0;
  56.       long t_numint(), t_numblk();
  57.       int  sam(), dave(), timer(), idle(), maintask;
  58.  
  59.       if( !(Queue1 = (T_QUEUE *) t_makequeue( 2 ) ) )
  60.           printf("Can't make Queue1 queue\n"), exit(1);
  61.  
  62.       if( !(Queue2 = (T_QUEUE *) t_makequeue( 2 ) ) )
  63.           printf("Can't make Queue2 queue\n"), exit(1);
  64.  
  65.     #if (TEST == 1)
  66.       status = (int) t_create(sam, "sam", 100, 512, "SAM", NULL);
  67.       status = (int) t_create(dave, "dave", 50, 512, "DAVE", NULL);
  68.       status = t_start( 0 );
  69.     #endif
  70.  
  71.     #if (TEST == 2)
  72.       status = (int) t_create(timer,"timer",10, 512,"timer",NULL);
  73.       status = (int) t_create(idle, "idle", 1, 100,    NULL);
  74.       status = t_start( 2 );
  75.     #endif
  76.  
  77.     #if (TEST == 3)
  78.       status = (int) t_create(maintask,"maintask",200, 512, NULL);
  79.       status = t_start( 2 );
  80.     #endif
  81.  
  82.       t_perror( "\ndone: ", status );
  83.       t_sstats();
  84.       printf("%ld interrupts, %ld blocked\n", t_numint(), t_numblk();
  85.     }
  86.  
  87. Example 3: A program that creates queues, starts multitasking, and performs thrs
  88.  
  89.  
  90.  
  91.     dave( arg )
  92.     char  *arg;
  93.     {
  94.       char *s;
  95.  
  96.       t_printf( "In dave(%s), about to wait for message\n", arg );
  97.       
  98.       if( t_iserr(s = t_wait(Queue1, 100)) )
  99.         t_perror("dave: first wait call", (int) s );
  100.       else
  101.         t_printf( "dave: got <%s> from Queue1\n", s );
  102.  
  103.       if( t_iserr(s = t_wait(Queue1, 100)) )
  104.         t_perror("dave: first wait call", (int) s );
  105.       else
  106.         t_printf( "dave: got %s from Queue1\n", s );
  107.  
  108.       t_printf("dave: yielding\n");
  109.       t_yield();
  110.  
  111.       t_printf("dave: deleting self\n");
  112.       t_delete( NULL );
  113.     }
  114.  
  115. Example 4: The task sam()
  116.  
  117.  
  118.  
  119.      sam( arg )
  120.      char  *arg;
  121.      {
  122.        int err;
  123.  
  124.        t_printf( "In sam(%s), yielding: no messages\n", arg);
  125.  
  126.        t_yield();
  127.  
  128.        t_printf( "sam: back from yield, sending messages\n");
  129.  
  130.        if( err = t_send( Queue1, "1st message") )
  131.          t_perror( "Foo: sending 1st message", err );
  132.  
  133.        if( err = t_send( Queue1, "2nd message") )
  134.          t_perror( "Foo: sending 2nd message", err );
  135.  
  136.        if( err = t_send( Queue1, "3rd message") )
  137.          t_perror( "Foo: sending 3rd message", err );
  138.  
  139.        t_printf("Foo: yielding again\n");
  140.        t_yield();
  141.        t_printf("Foo: Returned from 2nd yield, deleting self\n");
  142.        t_delete( NULL );
  143.      }
  144.  
  145. Example 5: The task dave()
  146.  
  147.  
  148.  
  149.      In sam(SAM), yielding: no messages
  150.      In dave(DAVE), about to wait for message
  151.      sam: back from yield, sending messages
  152.      dave: got <1st message> from Queue1
  153.      dave: got 2nd message from Queue1
  154.      dave: yielding
  155.      Foo: yielding again
  156.      dave: deleting self
  157.      Foo: Returned from 2nd yield, deleting self
  158.      done: No error
  159.  
  160.      Scheduler called 0 times: 0 tasks timed-out, 0 context swaps
  161.      0 interrupts, 0 blocked
  162.  
  163. Example 6: Output from test 1 in Example 3
  164.  
  165.  
  166.  
  167.            timer()
  168.            {
  169.              int i;
  170.  
  171.              t_printf("Starting up timer\n");
  172.  
  173.              for( i = 5; --i >= 0 ; )
  174.              {
  175.                t_printf( "!" ); 
  176.                t_wait( Queue2, t_second() );
  177.              }
  178.  
  179.              t_printf("Deleting timer\n");
  180.              t_delete( NULL );
  181.            }
  182.            #endif
  183.  
  184. Example 7: The timer() task
  185.  
  186.  
  187.  
  188.     timer( arg )
  189.     {
  190.       /* I'm assuming that pointers & ints are
  191.        * the same size here.
  192.        */
  193.  
  194.       int i;
  195.  
  196.       while( 1 )
  197.       {
  198.         for( i = 10000; --i >= 0 ; )
  199.             ;
  200.         t_cli();
  201.         dv_putchar( arg + '0' );
  202.         t_sti();
  203.       }
  204.     }
  205.  
  206.     maintask()
  207.     {
  208.       TCB *t1, *t2;
  209.  
  210.       t1 = t_create( timer, "1st timer", 100, 512, (void*)1, NULL);
  211.       t2 = t_create( timer, "2nd timer", 100, 512, (void*)2, NULL);
  212.  
  213.       t_perror( "task1:", (int)t1 );
  214.       t_perror( "task2:", (int)t2 );
  215.  
  216.       t_wait( Queue2, 5 * t_second() );
  217.  
  218.       t_delete( t1 );
  219.       t_delete( t2 );
  220.       t_delete( NULL );
  221.     }
  222.  
  223. Example 8: Test 3, used in Example 3
  224.  
  225.  
  226.  
  227.  
  228.         double mean(reset, data)
  229.         int   reset;
  230.         double data;
  231.         {
  232.           static double xhat, ki;
  233.  
  234.           return reset ? (ki = xhat = 0) 
  235.                  : (xhat += (data \sc0\ xhat) / ++ki)
  236.                  ;
  237.         }
  238.  
  239. Example 9: A C function that implements the algorithm shown in Figure 1
  240.